// /*!
//     \file    main.c
//     \brief   led spark with systick

//     \version 2024-12-20, V3.3.1, firmware for GD32F4xx
// */

// /*
//     Copyright (c) 2024, GigaDevice Semiconductor Inc.

//     Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:

//     1. Redistributions of source code must retain the above copyright notice, this
//        list of conditions and the following disclaimer.
//     2. Redistributions in binary form must reproduce the above copyright notice,
//        this list of conditions and the following disclaimer in the documentation
//        and/or other materials provided with the distribution.
//     3. Neither the name of the copyright holder nor the names of its contributors
//        may be used to endorse or promote products derived from this software without
//        specific prior written permission.

//     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
// OF SUCH DAMAGE.
// */

// #include "gd32f4xx.h"
// #include "systick.h"
// #include <stdio.h>
// #include "main.h"
// #include "gd32f450i_eval.h"

// /*!
//     \brief    toggle the led every 500ms
//     \param[in]  none
//     \param[out] none
//     \retval     none
// */
// // void led_spark(void)
// // {
// //     static __IO uint32_t timingdelaylocal = 0U;

// //     if(timingdelaylocal) {

// //         if(timingdelaylocal < 500U) {
// //             gd_eval_led_on(LED1);
// //         } else {
// //             gd_eval_led_off(LED1);
// //         }

// //         timingdelaylocal--;
// //     } else {
// //         timingdelaylocal = 1000U;
// //     }
// // }

// /*!
//     \brief    main function
//     \param[in]  none
//     \param[out] none
//     \retval     none
// */
// int main(void)
// {
// #ifdef __FIRMWARE_VERSION_DEFINE
//     uint32_t fw_ver = 0;
// #endif

//     // gd_eval_led_init(LED1);
//     systick_config();

// #ifdef __FIRMWARE_VERSION_DEFINE
//     fw_ver = gd32f4xx_firmware_version_get();
//     /* print firmware version */
//     printf("\r\nGD32F4xx series firmware version: V%d.%d.%d", (uint8_t)(fw_ver >> 24), (uint8_t)(fw_ver >> 16), (uint8_t)(fw_ver >> 8));
// #endif /* __FIRMWARE_VERSION_DEFINE */
//     printf("=============================\r\n");
//     usart_data_transmit(USART0, "=============================\r\n");
//     int i = 0;
//     while(1) {
//         i++;
//         printf("%d",i);
//     }
// }

// #ifdef GD_ECLIPSE_GCC
// /* retarget the C library printf function to the USART, in Eclipse GCC environment */
// int __io_putchar(int ch)
// {
//     usart_data_transmit(EVAL_COM0, (uint8_t)ch);
//     while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
//     return ch;
// }
// #else
// /* retarget the C library printf function to the USART */
// int fputc(int ch, FILE *f)
// {
//     usart_data_transmit(EVAL_COM0, (uint8_t)ch);
//     while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));

//     return ch;
// }
// #endif /* GD_ECLIPSE_GCC */


#include "gd32f4xx.h"
#include "systick.h"
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
// #include <sys/stat.h>
// #include <unistd.h>
#include "main.h"
#include "gd32f4xx_rcu.h"
#include "gd32f4xx_usart.h"
#include "los_task.h"

#define BKP_VALUE    0x32F0

static uint32_t RTCSRC_FLAG = 0;

__IO static uint32_t prescaler_a = 0, prescaler_s = 0;

void gpio_clock_init(void) 
{
	rcu_periph_clock_enable(RCU_GPIOA);
	rcu_periph_clock_enable(RCU_GPIOB);
	rcu_periph_clock_enable(RCU_GPIOC);
	rcu_periph_clock_enable(RCU_GPIOD);
	rcu_periph_clock_enable(RCU_GPIOE);
	rcu_periph_clock_enable(RCU_GPIOF);
	rcu_periph_clock_enable(RCU_GPIOG);
}


void rtc_pre_config(void)
{
    #if defined (RTC_CLOCK_SOURCE_IRC32K)
          rcu_osci_on(RCU_IRC32K);
          rcu_osci_stab_wait(RCU_IRC32K);
          rcu_rtc_clock_config(RCU_RTCSRC_IRC32K);

          prescaler_s = 0x13F;
          prescaler_a = 0x63;
    #elif defined (RTC_CLOCK_SOURCE_LXTAL)
          rcu_osci_on(RCU_LXTAL);
          rcu_osci_stab_wait(RCU_LXTAL);
          rcu_rtc_clock_config(RCU_RTCSRC_LXTAL);

          prescaler_s = 0xFF;
          prescaler_a = 0x7F;
    #else
    #error RTC clock source should be defined.
    #endif /* RTC_CLOCK_SOURCE_IRC32K */

    rcu_periph_clock_enable(RCU_RTC);
    rtc_register_sync_wait();
}

void rtc_setup(void)
{
    rtc_parameter_struct   rtc_initpara;
    rtc_initpara.factor_asyn = prescaler_a;
    rtc_initpara.factor_syn = prescaler_s;
    rtc_initpara.year = 0x24;
    rtc_initpara.day_of_week = RTC_MONDAY;
    rtc_initpara.month = RTC_JAN;
    rtc_initpara.date = 0x01;
    rtc_initpara.display_format = RTC_24HOUR;
    rtc_initpara.am_pm = RTC_AM;
    rtc_initpara.hour = 0;
    rtc_initpara.minute = 0;
    rtc_initpara.second = 0;

    if(ERROR == rtc_init(&rtc_initpara)){
    }else{
        RTC_BKP0 = BKP_VALUE;
    }
}

void rtc_config(void)
{
    rcu_periph_clock_enable(RCU_PMU);
    pmu_backup_write_enable();
    rtc_pre_config();
    RTCSRC_FLAG = GET_BITS(RCU_BDCTL, 8, 9);
    if((BKP_VALUE != RTC_BKP0) || (0x00 == RTCSRC_FLAG)){
        rtc_setup();
    }else{
        if (RESET != rcu_flag_get(RCU_FLAG_PORRST)){
        }else if (RESET != rcu_flag_get(RCU_FLAG_EPRST)){
        }
    }
}


void usart0_dma_config(void)
{	
	rcu_periph_clock_enable(RCU_USART0);
    gpio_af_set(UART0_TX_PORT, UART0_TX_AF, UART0_TX_PIN);
    gpio_af_set(UART0_RX_PORT, UART0_RX_AF, UART0_RX_PIN);
	
    gpio_mode_set(UART0_RX_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART0_RX_PIN);
    gpio_output_options_set(UART0_RX_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART0_RX_PIN);
    
    gpio_mode_set(UART0_TX_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, UART0_TX_PIN);
    gpio_output_options_set(UART0_TX_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, UART0_TX_PIN);
	
    usart_deinit(USART0);
    usart_baudrate_set(USART0, 115200U);
    usart_receive_config(USART0, USART_RECEIVE_ENABLE);
    usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    usart_enable(USART0);
    nvic_irq_enable(USART0_IRQn, 0, 0);
    usart_interrupt_enable(USART0, USART_INT_RBNE);
		
}

// int fputc(int ch, FILE *f)
// {
//     usart_data_transmit(USART0, (uint8_t)ch);
//     while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
//     return ch;
// }



UINT32 taskSample2();
VOID taskSampleEntry2(VOID)
{
  UINT32 i = 5;
  while(i > 0)
  {
    LOS_TaskDelay(1000);
    printf("taskSampleEntry2 running...\n");
    i--;
  }
  OHOS_SystemInit();
  printf("===========>OHOS_SystemInit() success!!!\n");
}

VOID taskSampleEntry1(VOID)
{
  // taskSample2();
  printf("Enter taskSampleEntry1...\n");
  UINT32 i = 5;
  while(i > 0)
  {
    LOS_TaskDelay(2000);
    printf("taskSampleEntry1 running...\n");
    i--;
  }
}

UINT32 taskSample1(VOID)
{
  printf("Enter taskSample1--->\n");
  UINT32 uwRet;
  UINT32 taskID1;
  TSK_INIT_PARAM_S stTask1={0};

  stTask1.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry1;
  stTask1.uwStackSize = 0x2000;
  stTask1.pcName = "taskSampleEntry1";
  stTask1.usTaskPrio = 6;

  uwRet = LOS_TaskCreate(&taskID1,&stTask1);
  if(uwRet != LOS_OK)
  {
    printf("task1 creat failed \n");
  }
    printf("--->Finish taskSample1 \n");

  return LOS_OK;
}

UINT32 taskSample2(VOID)
{
  printf("Enter taskSample2--->\n");
  UINT32 uwRet;
  UINT32 taskID2;
  TSK_INIT_PARAM_S stTask2={0};

  stTask2.pfnTaskEntry = (TSK_ENTRY_FUNC)taskSampleEntry2;
  stTask2.uwStackSize = 0x3000;
  stTask2.pcName = "taskSampleEntry2";
  stTask2.usTaskPrio = 9;

  uwRet = LOS_TaskCreate(&taskID2,&stTask2);
  if(uwRet != LOS_OK)
  {
    printf("task2 creat failed \n");
  }
    printf("--->Finish taskSample \n");

  return LOS_OK;
}
#include "lfs.h"
#include "utils_file.h"



/*!
    \brief    main function
    \param[in]  none
    \param[out] none
    \retval     none
*/
int main(void)
{
    int ret = -1;
    systick_config();
    gpio_clock_init();
    rtc_config();
    usart0_dma_config();
    w25q64_init_config();

    ret = LOS_KernelInit();

    LittlefsInit();
 
    if(ret == 0)
    {
	    printf("kernel init ok\n");
      // OHOS_SystemInit();
	    printf("OHOS_SystemInit ok\n");
    }


    taskSample1();
    taskSample2();

    LOS_Start();

    while(1) {
    }
}

